<template>
  <div>
  </div>
</template>

<script>
/* eslint-disable */
// import axios from 'axios'

export default {
  name: 'gSearch',
  data () {
    return {
      input: '',
      mode: '1',
      // 后台请求到的json数据
      // data: require('../data/combinedData.json'),
      data: [],
      results: [],
    }
  },
  mounted () {
    // this.data = this.getDataFromNeo4j()
    this.getDataFromNeo4j()
    // console.log("this.data", this.data)
    // this.$emit('getData', this.data)
  },
  methods: {
    getDataFromNeo4j() {
      const neo4j = require('neo4j-driver');
      const driver = neo4j.driver('bolt://localhost', neo4j.auth.basic('neo4j', '123456'));
      const session = driver.session({ defaultAccessMode: neo4j.session.READ });
      // const query = "MATCH (p)-[r]->(q) RETURN p, r, q";
      // const query = "MATCH (p)-[r]->(q) RETURN p, r, q LIMIT 30";
      const query = "MATCH (p)-[r]->(q) RETURN p, r, q LIMIT 300";
      const res = [];
      session
          .run(query)
          .subscribe({
              onKeys: keys => {
                  // console.log(keys)
              },
              onNext: record => {
                  const nodeData = record.get('p').properties;
                  const endNodeData = record.get('q').properties;
                  const relationshipData = record.get('r').properties;

                  const startIdentity = record.get('p').identity;
                  const endIdentity = record.get('q').identity;
                  const relationIdentity = record.get('r').identity;
          
                  const startLabel = record.get('p').labels;
                  const endLabel = record.get('q').labels;
          
                  const relationStart = record.get('r').start;
                  const relationEnd = record.get('r').end;
                  const relationType = record.get('r').type;

                  // 将节点数据、关系数据和终点节点数据合并到一个对象中
                  const combinedData = {
                      start: { 
                        startIdentity, 
                        nodeData, 
                        startLabel
                      },
                      end: { 
                        endIdentity, 
                        endNodeData,
                        endLabel
                      },
                      relationship: {
                        relationIdentity,
                        relationshipData,
                        relationStart,
                        relationEnd,
                        relationType
                      }
                  };
                  console.log(combinedData)
                  // this.data.push(combinedData);
                  res.push(combinedData);
              },
              onCompleted: response => {
                  session.close() // returns a Promise
                  this.data = res
                  // console.log(this.data)
                  this.$emit('getData', this.data)
              },
              onError: error => {
                  console.log(error)
              }
          })
          // return res
    },
    query () {
      // console.log(typeof this.mode)
      this.getDataFromNeo4j()
      // this.data = require('../data/combinedData.json')
      
    },
    querySearch (queryString, cb) {
      var res = this.results
      var results = queryString ? res.filter(this.createFilter(queryString)) : res
      // 调用 callback 返回建议列表的数据
      cb(results)
    },
    createFilter (queryString) {
      return (res) => {
        return (res.value.toLowerCase().indexOf(queryString.toLowerCase()) !== -1)
      }
    }
  },
  handleSelect (item) {
    console.log(item)
  }
}
</script>

<style lang='scss' scoped>
</style>
